package com.zeng.jiudian.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.zeng.jiudian.bean.UserModel;
import com.zeng.jiudian.service.UserService;
import com.zeng.jiudian.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author JJ_yo
 * @Date 2020/7/16-14:42
 */
@WebServlet("/userRegister")
public class UserRegisterServlet extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
    {
        //处理乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String introduce = req.getParameter("introduce");

        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassword(password);
        userModel.setCode(code);
        userModel.setPhone(phone);
        userModel.setSex(sex);
        userModel.setBirthday(birthday);
        userModel.setIntroduce(introduce);

        UserService userService = new UserServiceImpl();
        int affect = userService.addUser(userModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("affect", affect);   //将整型转换为json对象
        resp.getWriter().println(jsonObject);   //传递到页面的值

        //if (affect == 1)
        //{
        //    resp.sendRedirect("back/login.html");
        //}
        //else
        //{
        //    req.setAttribute("result", "注册失败");
        //    resp.sendRedirect("back/userRegister.jsp");
        //}
    }
}
